package com.frontend.screens;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.frontend.ConnectionException;
import com.frontend.DoctorsOfficeClient;
import com.data.documents.*;

import java.util.*;

/**
 * 
 * @author Elizabeth
 * @author Taylor
 * 
 */
public class ListInvoicesScreen extends Screen {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1868205419321633836L;
	private JLabel title, nameLabel, classLabel, unameLabel;
	private JList list;
	private JButton editDoc, back;
	private JScrollPane jsp;
	private UserDocument[] dList;
	private Screen previousScreen;

	public ListInvoicesScreen(DoctorsOfficeClient window, Screen previousScreen) {
		super(window);
		
		this.previousScreen = previousScreen;

		setLayout(null);
		SearchListener sl = new SearchListener(this);
		
		title = new JLabel("Choose Invoice to View:");
		title.setAlignmentX(CENTER_ALIGNMENT);
		
		nameLabel = new JLabel("Document Type:");
		classLabel = new JLabel("Date:");
		unameLabel = new JLabel("Notes:");

		list = new JList();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		list.setVisibleRowCount(-1);
		list.setAlignmentX(Component.CENTER_ALIGNMENT);
		list.setCellRenderer(new MyCellRenderer(new Font("Arial", Font.PLAIN,
				getHeight() / 20)));

		jsp = new JScrollPane(list);

		editDoc = new JButton("View Invoice");
		editDoc.setOpaque(false);
		editDoc.addActionListener(sl);
		editDoc.setMnemonic(KeyEvent.VK_V);

		back = new JButton("Back");
		back.setOpaque(false);
		back.addActionListener(sl);
		back.setMnemonic(KeyEvent.VK_B);
		
		add(title);
		add(nameLabel);
		add(classLabel);
		add(unameLabel);
		add(jsp);
		add(editDoc);
		add(back);

		search();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			int height = getHeight();
			int width = getWidth();
			Insets insets = getInsets();
			int curY = insets.top;
			
			title.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));
			FontMetrics fm = g.getFontMetrics(new Font("Arial", Font.BOLD, getHeight() / 15));
			int textWidth = (int)fm.getStringBounds(title.getText(), g).getWidth();
			title.setBounds(width/2 - textWidth/2 +insets.left,curY+=getHeight()/30,textWidth,height/10);
			curY += height/10;
			
			nameLabel.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));
			nameLabel.setBounds(width/10 + insets.left, curY + height / 30, width/2, height / 20);
			classLabel.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));
			classLabel.setBounds(width/10 + width * 4 / 15 + insets.left, curY + height / 30, width/2, height / 20);
			unameLabel.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));
			unameLabel.setBounds(width/10 + width * 8 / 15 + insets.left, curY += height / 30, width/2, height / 20);
			curY += height / 15;
			
			list.setCellRenderer(new MyCellRenderer(new Font("Arial",
					Font.PLAIN, getHeight() / 30)));
			
			jsp.setBounds(width/10 + insets.left, curY, width * 4 / 5, height * 5 / 8);
			curY += height * 5 / 8;
			curY += height / 30;

			editDoc.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			back.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));

			editDoc.setBounds(width / 2 + insets.left - width / 30 - width / 4, curY, width / 4, height/20);
			back.setBounds(width / 2 + insets.left + width / 30, curY, width / 4, height/20);

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}

	void search() {

		java.util.List<UserDocument> resultsList = null;
		try {
			resultsList = window.getNet().getDocuments(window.getCurrentUser().getUsername());
		} catch (ConnectionException e) {
			System.out.println(e.getMessage());
		}
		Object[] objArr = resultsList.toArray();
		int numInvoices = 0;
		for(int i = 0; i < objArr.length; i++){
			if((UserDocument)objArr[i] instanceof Invoice)
				numInvoices++;
		}
		UserDocument[] resultsArray = new UserDocument[numInvoices];
		int realIndex = 0;
		for(int i = 0; i < objArr.length; i++){
			if((UserDocument)objArr[i] instanceof Invoice){
				resultsArray[realIndex] = (UserDocument)objArr[i];
				realIndex++;
			}
		}
		if (resultsList != null) {
			Arrays.sort(resultsArray);
			dList = resultsArray;
			String[][] data = new String[resultsArray.length][3];
			for (int i = 0; i < data.length; i++)
				data[i] = resultsArray[i].getDataAsStrings();
			list.setListData(data);
		}
	}

	private class SearchListener implements ActionListener {

		private Screen superClass;
		
		public SearchListener(Screen superClass){
			super();
			this.superClass = superClass;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(back)) {
				window.changeScreen(previousScreen);
				previousScreen.repaint();
			} 
			else if (e.getSource().equals(editDoc) && list.getSelectedIndex() != -1){
				if(dList[list.getSelectedIndex()] instanceof Invoice){
					window.changeScreen(new ViewInvoiceScreen(window,(Invoice)dList[list.getSelectedIndex()], superClass));
				}
			}
		}
	}

	private class MyCellRenderer extends JPanel implements ListCellRenderer {
		/**
		 * 
		 */
		private static final long serialVersionUID = 2985185169127969497L;
		/**
		 * 
		 */
		JLabel left, middle, right;
		private Font renderFont;

		MyCellRenderer(Font renderFont) {
			setLayout(new GridLayout(1, 3));
			left = new JLabel();
			middle = new JLabel();
			right = new JLabel();
			left.setOpaque(true);
			middle.setOpaque(true);
			right.setOpaque(true);
			add(left);
			add(middle);
			add(right);
			this.renderFont = renderFont;
		}

		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			String leftData = ((String[]) value)[0];
			String middleData = ((String[]) value)[1];
			String rightData = ((String[]) value)[2];
			left.setText(leftData);
			middle.setText(middleData);
			right.setText(rightData);
			if (isSelected) {
				left.setBackground(list.getSelectionBackground());
				left.setForeground(list.getSelectionForeground());
				middle.setBackground(list.getSelectionBackground());
				middle.setForeground(list.getSelectionForeground());
				right.setBackground(list.getSelectionBackground());
				right.setForeground(list.getSelectionForeground());
			} else {
				left.setBackground(list.getBackground());
				left.setForeground(list.getForeground());
				middle.setBackground(list.getBackground());
				middle.setForeground(list.getForeground());
				right.setBackground(list.getBackground());
				right.setForeground(list.getForeground());
			}
			setEnabled(list.isEnabled());
			left.setFont(renderFont);
			middle.setFont(renderFont);
			right.setFont(renderFont);
			setMaximumSize(new Dimension(getWidth() * 4 / 5,
					getHeight() * 1 / 2));
			return this;
		}
	}

}
